/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.a1anwang.okble.demo.widget;

import com.a1anwang.okble.demo.ResourceTable;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

public class HeadDefaultComponent extends HeadBaseComponent {

    private Image mImage;
    private Text mText;
    private AnimatorProperty mAnimator;
    /**
     * HeadDefaultComponent
     *
     * @param context context
     * */
    public HeadDefaultComponent(Context context) {
        this(context, null);
    }
    /**
     * HeadDefaultComponent
     *
     * @param context context
     * @param attrSet attrSet
     * */
    public HeadDefaultComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");

    }
    /**
     * HeadDefaultComponent
     *
     * @param context context
     * @param attrSet attrSet
     * @param styleName styleName
     * */
    public HeadDefaultComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    @Override
    public void init() {
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_head_default_layout, this, true);
        mImage = (Image) findComponentById(ResourceTable.Id_image);
        mText = (Text) findComponentById(ResourceTable.Id_text);
    }

    @Override
    public void onHeadVisible() {
        mText.setText("下拉刷新");
    }

    @Override
    public void onHeadOver() {
        mText.setText("松开刷新");
    }

    @Override
    public void onRefresh() {
        mText.setText("正在刷新...");
        mAnimator = mImage.createAnimatorProperty();
        mAnimator.setDuration(2000).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mImage).start();
    }

    @Override
    public void onFinish() {
        mText.setText("刷新成功");
        mAnimator.cancel();
    }
}
